package pactera.tf.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import pactera.tf.dataobj.*;
import pactera.tf.dto.ChatGroupDTO;
import pactera.tf.excpetion.R;
import pactera.tf.service.GroupChatMsgService;
import pactera.tf.service.GroupChatService;
import pactera.tf.service.GroupInfoService;
import pactera.tf.service.LoginService;

@Controller
public class GroupChatController {
    @Autowired
    GroupInfoService  groupInfoService;
    @Autowired
    GroupChatMsgService groupChatMsgService;

    @Autowired
    private GroupChatService groupChatService ;

    @Autowired
    private LoginService loginService;

    
    /**
     * 
    
     * <p>Title: listGroup</p>  
    
     * <p>Description:查询当前用户所属的群组 </p>  
    
     * @param session
     * @return
     */
    @PostMapping("/chat/listGroup")
    @ResponseBody 
    public R listGroup(HttpSession session){
        String userid=(String)session.getAttribute("userid");
        List<GroupInfoDO> userGroup = groupInfoService.listAllGroup(userid);
        return R.ok().data("userGroup", userGroup).message("群组信息");
    }
    /**
     * 
    
     * <p>Title: lkfriends</p>  
    
     * <p>Description:查询当前群的历史聊天记录 </p>  
    
     * @param acgroupid
     * @return  聊天信息的List集合 
     */
    @PostMapping("/chat/listgroupchatmsg/{acgroupid}")
    @ResponseBody 
    public  R listgroupchatmsg(@PathVariable("acgroupid")String acgroupid){
    	List<GroupChatMsgDO> msgList = groupChatMsgService.listAllMsg(acgroupid);
    	return R.ok().data("groupChatMsg", msgList).message("群聊信息");
    }


    /**
     * 创建群组信息
     *
     */
    @PostMapping(value="/chat/createGroupInfo",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R createGroupInfo(@RequestBody GroupInfoDO groupInfoDO, HttpServletRequest request){
        Object userid = request.getSession().getAttribute("userid");
        LoginDO loginDO = loginService.getLoginByUserid(String.valueOf(userid));
        ChatGroupDTO chatGroupDTO = new ChatGroupDTO();
        chatGroupDTO.setUserid(loginDO.getUserid());
        chatGroupDTO.setGroupusernickname(loginDO.getUsername());
        chatGroupDTO.setGroupname(groupInfoDO.getGroupName());
        chatGroupDTO.setGroupcreaterid(loginDO.getUserid());
        int result = groupInfoService.createGroupInfo(chatGroupDTO);

        if(result == 1){
            return R.ok().message("创建群成功");
        }else{
            return R.error().message("创建群失败");
        }

    }

    /**
     * 创建群组信息
     *
     */
    @PostMapping(value="/chat/searchGroupInfo",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R searchGroupInfo(@RequestBody GroupInfoDO groupInfo){
        GroupInfoDO groupInfoDO = groupInfoService.getSearchGroupByName(groupInfo.getGroupName());
        if(groupInfoDO == null){
            return R.error().message("群信息不存在");
        }
        return R.ok().data("groupinfo",groupInfoDO).message("群信息");

    }

    /**
     * 添加到群
     *
     */
    @PostMapping(value="/chat/addToChatGroup",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R addToChatGroup(@RequestBody GroupChatDO chatGroupDO,HttpServletRequest request){
        Object userid = request.getSession().getAttribute("userid");
        LoginDO loginDO = loginService.getLoginByUserid(String.valueOf(userid));
        chatGroupDO.setUserId(loginDO.getUserid());

        //查询用户是否已在群里
        GroupChatDO groupInfoByKey = groupChatService.getGroupInfoByKey(chatGroupDO);
        if(groupInfoByKey != null){
            return R.error().message("您已经在该群中，无需再次添加");
        }else {
            chatGroupDO.setGroupUserNickName(loginDO.getUsername());
            groupChatService.addUserToChatGroup(chatGroupDO);
        }

        return R.ok().message("加群成功");

    }
    /**
     * 
    
     * <p>Title: getGroupUserNickName</p>  
    
     * <p>Description:获取群成员昵称 </p>  
    
     * @param sendUserIdAndGroupId
     * @return
     */
    @PostMapping("/chat/getNickName/{sendUserIdAndGroupId}")
    @ResponseBody 
    public R getGroupUserNickName(@PathVariable("sendUserIdAndGroupId")String sendUserIdAndGroupId) {
    	String[] idArr= sendUserIdAndGroupId.split("&");
    	String sendUserId = "";
    	String groupId = "";
    	if (null != idArr && idArr.length == 2) {
    		 sendUserId = idArr[0];
    		 groupId = idArr[1];
    	}
    	String nickName = groupChatMsgService.getGroupUserNickName(sendUserId,groupId);
		return R.ok().data("nickName",nickName).message("用户昵称");
    }
    /**
     * 
    
     * <p>Title: getUserImg</p>  
    
     * <p>Description:获取群成员头像 </p>  
    
     * @param sendUserId
     * @return
     */
    @PostMapping("/chat/getUserImg/{sendUserId}")
    @ResponseBody 
    public R getUserImg(@PathVariable("sendUserId")String sendUserId) {
    	String userImg = groupChatMsgService.getUserImg(sendUserId);
		return R.ok().data("UserImg",userImg).message("用户头像");
    }
    
}
